//登录协议管理
class LoginPrase{
    private static _instance:LoginPrase = null;
    private _isAccredit:boolean = false;
	/** 
	 * 是否授权
	 */
	public get isAccredit(){
		return this._isAccredit;
	}


    constructor()
    {

    }

    public static instance()
    {
        if(!this._instance)
        {
            this._instance = new LoginPrase();
        }
        return this._instance;
    }

    //登录
    public Login()
    {
        this.giftPapaerAc();
        WeChatgGameManager.instance().getUserInfo((info)=>{
            this._isAccredit = true;
            WeChatgGameManager.instance().hideUserInfoButton();
            this.postRoleInfo();
        }, this); 
    }

    public giftPapaerAc()
    {
        //一个人只能领取一次
        let isGetPaper = RoleCache.instance().judgeIsAreadyGetPaper();
        if(!isGetPaper)
        {
            let showData = WeChatgGameManager.instance().getOnShowData();
            if(showData)    
            {
                showData.then((e)=>{
                    if(e && e['query'] && e['query']['canshu'] && e['query']['dt'] && e['query']['openid'])
                    {
                        let equipment_no = e['query']['canshu'];
                        let dt = e['query']['dt'];
                        let chuzhi_openid = e['query']['openid'];
                        WeChatgGameManager.instance().giftPaper(equipment_no, dt, chuzhi_openid);
                    }
                })
            }
        }
    }

    //请求玩家数据
    public postRoleInfo()
    {
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : '' ;
        let userInfo = WeChatgGameManager.instance().userInfo;
        let nick_name = "";
        let avatar_url = "";
        if(userInfo)
        {   
            nick_name = userInfo['nickName'] != undefined ? userInfo['nickName']: "";
            avatar_url = userInfo['avatarUrl'] != undefined ? userInfo['avatarUrl']: "";
        }
        
        let user = {
            openid:openid,
            nick_name:nick_name,
            avatar_url:avatar_url,
            version:GameConst.version
        }
        
        HttpManager.instance().sendPost(GameConst.httpUrl + 'fishingaccs/getUserInfo', StringUtil.jsonToHttpPostString(user), (e)=>{
            let data = JSON.parse(e);

            LogUtil.log(data.data);
            let showData =   WeChatgGameManager.instance().getOnShowData();

            // console.log(data);
            
            if(showData)    
            {
                showData.then((e)=>{
                if(e && e['query'] && e['query']['position'])
                {
                    let queryData = {
                        fromUser : e['query']['fromUser'],
                        position : e['query']['position'],
                        toUser: RoleCache.instance().openid
                    }
                    HttpManager.instance().sendPost(GameConst.httpUrl + 'options/get_task_complete' , StringUtil.jsonToHttpPostString(queryData), (e)=>{
                            let data = JSON.parse(e);
                            
                            if(data.msg == 'ok'){
                            }
                        })
                    } 
                }
                )
            }  
            
            // RoleCache.instance().data = data.data;
            
            WeChatgGameManager.instance().sendALDEvent('versionFrom', 'version', GameConst.version);
            RoleCache.instance().saveRoleinfo(data.data);
            UIManager.instance().hideNormalLoading();
            UIManager.instance().closeUILogic('LoadingUI');
            UIManager.instance().openUILogic(MainUI);

        }, this);
    }

    //登出
    public LogOut()
    {
        let openid = RoleCache.instance().openid ? RoleCache.instance().openid : '' ;
        HttpManager.instance().sendPost(GameConst.httpUrl + 'options/update_logout_time', 'openid=' + openid, (e)=>{
            
        }, this);
    }
}